import React from "react";
import routeWithUserSession from "@/components/ApplicationArea/routeWithUserSession";
import Button from "antd/lib/button";
import Col from "antd/lib/col";
import Select from 'antd/lib/select';
import Divider from "antd/lib/divider";
import Row from "antd/lib/row";
import Table from "antd/lib/table";
import Menu from "antd/lib/menu";
import Dropdown from "antd/lib/dropdown";
import ConfigProvider from "antd/lib/config-provider";
import Layout from "antd/lib/layout";
import AutoComplete from "antd/lib/auto-complete";
import zhCN from 'antd/es/locale/zh_CN';
import Employee from "@/services/employee";
import Enums from "@/services/enums";
import { currentUser } from "@/services/auth";
import routes from "@/services/routes";
import QueryShowField from "@/services/queryshowfield";
import { AppstoreFilled, DownOutlined, UpOutlined } from "@ant-design/icons";
import DatePicker from "antd/lib/date-picker";
import locale from 'antd/es/date-picker/locale/zh_CN';
import CreateQueryShowFieldDialog from "@/components/enums/QueryShowFieldDialog";
import EmployeeActivityDialog from "@/components/employee/EmployeeActivityDialog";
import Customer from "@/services/customer";

const getRowClassName = (record, index) => {
  let className = '';
  if (index % 2 === 0) className = "oddRow";
  return className
}

class CertificateList extends React.Component {
  state = {
    page: 1,
    pagesize: localStorage.getItem("certificatelistpagesize") ? (localStorage.getItem("certificatelistpagesize") !== undefined ? Number(localStorage.getItem("certificatelistpagesize")) : 20) : 20,
    total: 1,
    order: 1,
    loading: false,
    code: localStorage.getItem("certificatelistcode") ? (localStorage.getItem("certificatelistcode") !== undefined ? localStorage.getItem("certificatelistcode") : '') : '',
    name: localStorage.getItem("certificatelistname") ? (localStorage.getItem("certificatelistname") !== undefined ? localStorage.getItem("certificatelistname") : '') : '',
    major: localStorage.getItem("certificatelistmajor") ? (localStorage.getItem("certificatelistmajor") !== undefined ? localStorage.getItem("certificatelistmajor") : '') : '',
    area: localStorage.getItem("certificatelistarea") ? (localStorage.getItem("certificatelistarea") !== undefined ? localStorage.getItem("certificatelistarea") : '') : '',
    bytstatus: "",
    certificate: localStorage.getItem("certificatelistcertificate") ? (localStorage.getItem("certificatelistcertificate") !== undefined ? localStorage.getItem("certificatelistcertificate") : '') : '',
    location: localStorage.getItem("certificatelistlocation") ? (localStorage.getItem("certificatelistlocation") !== undefined ? localStorage.getItem("certificatelistlocation") : '') : '',
    mobile: localStorage.getItem("certificatelistmobile") ? (localStorage.getItem("certificatelistmobile") !== undefined ? localStorage.getItem("certificatelistmobile") : '') : '',
    idcard: localStorage.getItem("certificatelistidcard") ? (localStorage.getItem("certificatelistidcard") !== undefined ? localStorage.getItem("certificatelistidcard") : '') : '',
    periodunit: localStorage.getItem("certificatelistperiodunit") ? (localStorage.getItem("certificatelistperiodunit") !== undefined ? localStorage.getItem("certificatelistperiodunit") : '') : '',
    genjindate: [],
    contacts: localStorage.getItem("certificatelistcontacts") ? (localStorage.getItem("certificatelistcontacts") !== undefined ? localStorage.getItem("certificatelistcontacts") : '') : '',
    contactsmobile: localStorage.getItem("certificatelistcontactsmobile") ? (localStorage.getItem("certificatelistcontactsmobile") !== undefined ? localStorage.getItem("certificatelistcontactsmobile") : '') : '',
    userid: "",
    maintainer: "",
    data: [],
    employeestatusdata: [],
    employeemajordata: [],
    employeecertificatedata: [],
    employeeareadata: [],
    queryshowfielddata: [],
    selectedrowkeys: [],
    selectedrows: [],
    employeeuserdata: [],
    showall: false,
    periodunitdata: [{ id: "年", name: "年" }, { id: "半年", name: "半年" }, { id: "月", name: "月" }],
  };
  componentWillMount() {
    Enums.items("employeestatus").then(data => {
      this.setState({
        employeestatusdata: data,
      });
    });
    Enums.items("employeemajor").then(data => {
      this.setState({
        employeemajordata: data,
      });
    });
    Enums.items("employeecertificate").then(data => {
      this.setState({
        employeecertificatedata: data,
      });
    });
    Enums.items("employeearea").then(data => {
      this.setState({
        employeeareadata: data,
      });
    });
    Customer.querycustomeruser({ code: "", name: "", userid: currentUser.id, }).then(data => {
      this.setState({
        employeeuserdata: data,
      });
    });
    this.fieldsshowload();
    let param = {
      page: this.state.page,
      pagesize: this.state.pagesize,
      code: this.state.code,
      name: this.state.name,
      major: this.state.major,
      area: this.state.area,
      bytstatus: this.state.bytstatus,
      certificate: this.state.certificate,
      location: this.state.location,
      contacts: this.state.contacts,
      contactsmobile: this.state.contactsmobile,
      mobile: this.state.mobile,
      idcard: this.state.idcard,
      periodunit: this.state.periodunit,
      userid: this.state.userid,
      maintainer: this.state.maintainer,
      openseapool: 0,
      id: 0,
      datadown: false,
      projectsel: 1,
    }
    this.seldata(param);
  }
  handleclear = () => {
    this.setState({
      code: "",
      name: "",
      major: "",
      area: "",
      bytstatus: "",
      certificate: "",
      location: "",
      contacts: "",
      contactsmobile: "",
      mobile: "",
      idcard: "",
      periodunit: "",
      genjindate: [],
      userid: "",
      maintainer: "",
    });
    localStorage.setItem("certificatelistcode", "");
    localStorage.setItem("certificatelistname", "");
    localStorage.setItem("certificatelistmajor", "");
    localStorage.setItem("certificatelistarea", "");
    localStorage.setItem("certificatelistcertificate", "");
    localStorage.setItem("certificatelistlocation", "");
    localStorage.setItem("certificatelistmobile", "");
    localStorage.setItem("certificatelistidcard", "");
    localStorage.setItem("certificatelistperiodunit", "");
    localStorage.setItem("certificatelistcontacts", "");
    localStorage.setItem("certificatelistcontactsmobile", "");
  }
  fieldsshowload = () => {
    QueryShowField.getbycode("employeecertificatelist1").then(data => {
      this.setState({
        queryshowfielddata: data,
      });
    });
  }
  isEmptyobj = (str) => {
    if (str === '' || str === null || str === 'null' || str === undefined || str === 'undefined') {
      return true;
    } else {
      return false;
    }
  }
  seldata = (params = {}) => {
    this.setState({ loading: true });
    if (!this.isEmptyobj(this.state.genjindate)) {
      if (this.state.genjindate.length === 2) {
        params.genjinbegindate = this.state.genjindate[0].format('YYYY-MM-DD');
        params.genjinenddate = this.state.genjindate[1].format('YYYY-MM-DD');
      }
    }
    Employee.getlist(params).then(data => {
      this.setState({
        loading: false,
        data: data.data,
        page: params.page,
        pagesize: params.pagesize,
        total: data.total,
        field: params.field,
        order: params.order,
      });
    });
  };

  handlesel = (index) => {
    let datadown = false;
    let param = {
      page: 1,
      pagesize: this.state.pagesize,
      code: this.state.code,
      name: this.state.name,
      major: this.state.major,
      area: this.state.area,
      bytstatus: this.state.bytstatus,
      certificate: this.state.certificate,
      location: this.state.location,
      contacts: this.state.contacts,
      contactsmobile: this.state.contactsmobile,
      mobile: this.state.mobile,
      idcard: this.state.idcard,
      periodunit: this.state.periodunit,
      userid: this.state.userid,
      maintainer: this.state.maintainer,
      openseapool: 0,
      id: 0,
      datadown: datadown,
      projectsel: 1,
    }
    this.seldata(param);
  };
  handleTableChange = (pagination, filters, sorter) => {
    localStorage.setItem("certificatelistpagesize", pagination.pageSize);
    let field = sorter.field;
    let order = 0;
    if (sorter.order === "descend") {
      order = 1;
    }
    let param = {
      field: field,
      order: order,
      page: pagination.current,
      pagesize: this.state.pagesize,
      code: this.state.code,
      name: this.state.name,
      major: this.state.major,
      area: this.state.area,
      bytstatus: this.state.bytstatus,
      certificate: this.state.certificate,
      location: this.state.location,
      contacts: this.state.contacts,
      contactsmobile: this.state.contactsmobile,
      mobile: this.state.mobile,
      idcard: this.state.idcard,
      periodunit: this.state.periodunit,
      userid: this.state.userid,
      maintainer: this.state.maintainer,
      openseapool: 0,
      id: 0,
      datadown: false,
      projectsel: 1,
    }
    this.seldata(param);
  };
  handlefieldshow = () => {
    CreateQueryShowFieldDialog.showModal({ queryshowfield: this.state.queryshowfielddata })
      .onClose((result = {}) => {
        if (result.success) {
          this.fieldsshowload();
        }
      }).onDismiss(() => {
      });
  }
  handleactivityshow = (record) => {
    EmployeeActivityDialog.showModal({ sourceid: record.id, sourcetype: "employee", canedit: false })
      .onClose((result = {}) => {
      }).onDismiss(() => {
      });
  };
  onDragEnter = info => {
    window.console.log(info);
  };
  render() {
    const paginationProps = {
      showSizeChanger: true,
      total: this.state.total, // 数据总数
      pageSize: parseInt(this.state.pagesize), // 每页条数
      current: parseInt(this.state.page), // 当前页码
      pageSizeOptions: ["10", "20", "30", "100"],
      showTotal: ((total) => {
        return `共 ${total} 条`;
      }),
    };

    let listcolumns = [];
    if (this.state.queryshowfielddata.fieldsshow !== undefined) {
      (this.state.queryshowfielddata.fieldsshow || []).map(item => {
        let items = this.state.queryshowfielddata.fieldsall.filter((p) => {
          return p.key === item;
        });
        if (items.length > 0) {
          listcolumns.push({
            title: items[0].title,
            dataIndex: item,
            key: item,
            sorter: (a, b) => a[item] > b[item] ? 1 : -1
          })
        }
        return item;
      }
      );
    }
    listcolumns.push({
      title: '操作',
      key: 'action',
      render: (record) => (
        <span>
          <Button size="small" onClick={() => this.handleactivityshow(record)}>
            跟进信息
          </Button>
        </span>
      ),
    });
    return (
      <ConfigProvider locale={zhCN}>
        <div style={{ width: "400px", height: "60px" }}>
          <div style={{ width: "20px", height: "50px", float: "left" }}>
          </div>
          <div style={{ width: "50px", height: "50px", float: "left" }}>
            <img src="/static/images/certificatelist.png" alt="HR" loading="lazy" />
          </div>
          <div style={{ width: "320px", height: "30px", float: "left" }}>
            <h3 style={{ margin: "7px", padding: "0" }}>{"人员证书信息列表"}</h3>
          </div>
        </div>
        <Layout>
          <Layout style={{ padding: '0 24px 24px' }}>
            {this.state.showall && (<><Row style={{ marginBottom: 5 }}>
              {/* <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                状态:</div></Col>
              <Col span={6}>
                <AutoComplete
                  style={{ width: "100%" }}
                  value={this.state.bytstatus}
                  onChange={value => {
                    localStorage.setItem("certificatelistbytstatus", value);
                    this.setState({ bytstatus: value });
                  }}
                  placeholder="状态"
                  filterOption={(inputValue, option) =>
                    option.value.indexOf(inputValue) !== -1}>
                  {(this.state.employeestatusdata || []).map(item => (
                    <AutoComplete.Option key={item.key} value={item.key}>{item.key}</AutoComplete.Option>
                  ))}
                </AutoComplete>
              </Col> */}
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                证书:</div></Col>
              <Col span={6}>
                <AutoComplete
                  style={{ width: "100%" }}
                  value={this.state.certificate}
                  onChange={value => {
                    localStorage.setItem("certificatelistcertificate", value);
                    this.setState({ certificate: value });
                  }}
                  placeholder="证书"
                  filterOption={(inputValue, option) =>
                    option.value.indexOf(inputValue) !== -1}>
                  {(this.state.employeecertificatedata || []).map(item => (
                    <AutoComplete.Option key={item.key} value={item.key}>{item.key}</AutoComplete.Option>
                  ))}
                </AutoComplete>
              </Col>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                地区:</div></Col>
              <Col span={6}>
                <AutoComplete
                  style={{ width: "100%" }}
                  value={this.state.location}
                  onChange={value => {
                    localStorage.setItem("certificatelistlocation", value);
                    this.setState({ location: value });
                  }}
                  placeholder="地区"
                  filterOption={(inputValue, option) =>
                    option.value.indexOf(inputValue) !== -1}>
                  {(this.state.employeeareadata || []).map(item => (
                    <AutoComplete.Option key={item.key} value={item.key}>{item.key}</AutoComplete.Option>
                  ))}
                </AutoComplete>
              </Col>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                毕业专业:</div></Col>
              <Col span={6}>
                <AutoComplete
                  style={{ width: "100%" }}
                  value={this.state.major}
                  onChange={value => {
                    localStorage.setItem("certificatelistmajor", value);
                    this.setState({ major: value });
                  }}
                  placeholder="毕业专业"
                  filterOption={(inputValue, option) =>
                    option.value.indexOf(inputValue) !== -1}>
                  {(this.state.employeemajordata || []).map(item => (
                    <AutoComplete.Option key={item.key} value={item.key}>{item.key}</AutoComplete.Option>
                  ))}
                </AutoComplete>
              </Col>
            </Row>
              <Row style={{ marginBottom: 5 }}>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  手机:</div></Col>
                <Col span={6}>
                  <AutoComplete
                    style={{ width: "100%" }}
                    value={this.state.mobile}
                    onChange={value => {
                      localStorage.setItem("certificatelistmobile", value);
                      this.setState({ mobile: value });
                    }}
                    placeholder="手机"
                    filterOption={(inputValue, option) =>
                      option.value.indexOf(inputValue) !== -1}>
                  </AutoComplete>
                </Col>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  身份证号:</div></Col>
                <Col span={6}>
                  <AutoComplete
                    style={{ width: "100%" }}
                    value={this.state.idcard}
                    onChange={value => {
                      localStorage.setItem("certificatelistidcard", value);
                      this.setState({ idcard: value });
                    }}
                    placeholder="身份证号"
                    filterOption={(inputValue, option) =>
                      option.value.indexOf(inputValue) !== -1}>
                  </AutoComplete>
                </Col>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  社保地区:</div></Col>
                <Col span={6}>
                  <AutoComplete
                    style={{ width: "100%" }}
                    value={this.state.area}
                    onChange={value => {
                      localStorage.setItem("certificatelistarea", value);
                      this.setState({ area: value });
                    }}
                    placeholder="社保地区"
                    filterOption={(inputValue, option) =>
                      option.value.indexOf(inputValue) !== -1}>
                    {(this.state.employeeareadata || []).map(item => (
                      <AutoComplete.Option key={item.key} value={item.key}>{item.key}</AutoComplete.Option>
                    ))}
                  </AutoComplete>
                </Col>
              </Row>
              <Row style={{ marginBottom: 5 }}>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  期限单位:</div></Col>
                <Col span={6}>
                  <AutoComplete
                    style={{ width: "100%" }}
                    value={this.state.periodunit}
                    onChange={value => {
                      localStorage.setItem("employeelistperiodunit", value);
                      this.setState({ periodunit: value });
                    }}
                    placeholder="期限单位"
                    filterOption={(inputValue, option) =>
                      option.value.indexOf(inputValue) !== -1}>
                    {(this.state.periodunitdata || []).map(item => (
                      <AutoComplete.Option key={item.id} value={item.id}>{item.name}</AutoComplete.Option>
                    ))}
                  </AutoComplete>
                </Col>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  跟进日期:</div></Col>
                <Col span={6}>
                  <DatePicker.RangePicker
                    locale={locale}
                    style={{ width: "100%" }}
                    value={this.state.genjindate}
                    onChange={value => {
                      this.setState({
                        genjindate: value,
                      });
                    }}
                    placeholder="跟进日期"
                    autoFocus
                  />
                </Col>
              </Row>
              <Row style={{ marginBottom: 5 }}>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                联系人:</div></Col>
                <Col span={6}>
                  <AutoComplete
                    style={{ width: "100%" }}
                    value={this.state.contacts}
                    onChange={value => {
                      localStorage.setItem("employeelistcontacts", value);
                      this.setState({ contacts: value });
                    }}
                    placeholder="联系人"
                    filterOption={(inputValue, option) =>
                      option.value.indexOf(inputValue) !== -1}>
                  </AutoComplete>
                </Col>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                联系方式:</div></Col>
                <Col span={6}>
                  <AutoComplete
                    style={{ width: "100%" }}
                    value={this.state.contactsmobile}
                    onChange={value => {
                      localStorage.setItem("employeelistcontactsmobile", value);
                      this.setState({ contactsmobile: value });
                    }}
                    placeholder="联系方式"
                    filterOption={(inputValue, option) =>
                      option.value.indexOf(inputValue) !== -1}>
                  </AutoComplete>
                </Col>
              </Row>
              <Row style={{ marginBottom: 5 }}>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  维护人:</div></Col>
                <Col span={6}>
                  <Select style={{ width: "100%" }}
                    showSearch
                    allowClear
                    optionFilterProp="children"
                    value={this.state.maintainer}
                    onChange={value => {
                      this.setState({ maintainer: value });
                    }}>
                    {(this.state.employeeuserdata || []).map(item => (
                      <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>
                    ))}
                  </Select>
                </Col>
                <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                  录入人:</div></Col>
                <Col span={6}>
                  <Select style={{ width: "100%" }}
                    showSearch
                    allowClear
                    optionFilterProp="children"
                    value={this.state.userid}
                    onChange={value => {
                      this.setState({ userid: value });
                    }}>
                    {(this.state.employeeuserdata || []).map(item => (
                      <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>
                    ))}
                  </Select>
                </Col>
              </Row>
            </>)}
            <Row style={{ marginBottom: 5 }}>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                人员编码:</div></Col>
              <Col span={6}>
                <AutoComplete
                  style={{ width: "100%" }}
                  value={this.state.code}
                  onChange={value => {
                    localStorage.setItem("certificatelistcode", value);
                    this.setState({ code: value });
                  }}
                  placeholder="人员编码"
                  filterOption={(inputValue, option) =>
                    option.value.indexOf(inputValue) !== -1}>
                </AutoComplete>
              </Col>
              <Col span={2}><div style={{ marginTop: "5px", textAlign: "right", marginRight: "10px" }}>
                人员名称:</div></Col>
              <Col span={6}>
                <AutoComplete
                  style={{ width: "100%" }}
                  value={this.state.name}
                  onChange={value => {
                    localStorage.setItem("certificatelistname", value);
                    this.setState({ name: value });
                  }}
                  placeholder="人员名称"
                  filterOption={(inputValue, option) =>
                    option.value.indexOf(inputValue) !== -1}>
                </AutoComplete>
              </Col>
              <Col span={8}>
                <Divider type="vertical" />
                <Button type="primary" onClick={() => this.handlesel(1)} >
                  <i className="fa fa-search m-r-5" />
                  查询
                </Button>
                <Divider type="vertical" />
                <Button onClick={() => this.handleclear()} >
                  清空
                </Button>
                <Divider type="vertical" />
                <Button onClick={() => { this.setState({ showall: !this.state.showall }); }} >
                  {this.state.showall && (<UpOutlined />)}
                  {!this.state.showall && (<DownOutlined />)}
                  {this.state.showall ? '默认' : '更多'}
                </Button>
              </Col>
            </Row>
            <Row style={{ marginBottom: 5 }}>
              <Col span={10}>
                <Divider type="vertical" />
                <Dropdown
                  trigger={["click"]}
                  overlay={
                    <Menu>
                      {currentUser.hasPermission("fieldshow") && (
                        <Menu.Item key="delete-item">
                          <a onMouseUp={() => this.handlefieldshow()}>显示隐藏列</a>
                        </Menu.Item>
                      )}
                    </Menu>
                  }>
                  <Button type="primary" style={{ margin: "10px" }} data-test="CreateButton" >
                    <AppstoreFilled />更多功能
                  </Button>
                </Dropdown>
              </Col>
            </Row>
            <div>
              <Table rowKey='id'
                rowClassName={(record, index) => getRowClassName(record, index)}
                columns={listcolumns}
                scroll={{ x: 'max-content' }}
                dataSource={this.state.data}
                pagination={paginationProps}
                loading={this.state.loading}
                onChange={this.handleTableChange}
              />
            </div>
          </Layout>
        </Layout>
      </ConfigProvider>
    );
  }
}

routes.register(
  "Certificate.List",
  routeWithUserSession({
    path: "/certificate",
    title: "证书信息列表",
    render: pageProps => <CertificateList {...pageProps} />,
  })
);
